//
//  YHScanCardViewController.m
//  ocrcard
//
//  Created by jyh on 2018/5/17.
//  Copyright © 2018年 huig. All rights reserved.
//

#import "YHScanCardViewController.h"
#import "YHScanCardView.h"
#import "ViewController.h"
@interface YHScanCardViewController ()
@property (nonatomic, strong) YHScanCardView *scanCardView;
@end

@implementation YHScanCardViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.scanManager doSomethingWhenWillDisappear];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.scanManager doSomethingWhenWillAppear];
}

- (YHScanManager *)scanManager {
    if (!_scanManager) {
        _scanManager = [[YHScanManager alloc] init];
    }
    return _scanManager;
}

- (YHScanCardView *)scanCardView {
    if (!_scanCardView) {
        CGRect rect = [YHScanCardView getOverlayFrame:[UIScreen mainScreen].bounds];
        _scanCardView = [[YHScanCardView alloc] initWithFrame:rect];
    }
    return _scanCardView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view insertSubview:self.scanCardView atIndex:0];
    
    self.scanManager.sessionPreset = AVCaptureSessionPreset1280x720;
    
    if ([self.scanManager configBankScanManager]) {
        
        UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
        [self.view insertSubview:view atIndex:0];
        AVCaptureVideoPreviewLayer *preLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.scanManager.captureSession];
        preLayer.frame = [UIScreen mainScreen].bounds;
        
        preLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        
        [view.layer addSublayer:preLayer];
        
        [self.scanManager startSession];
        
    }
    else {
 
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    self.scanManager.getCardBlock = ^(CardModel *model) {
        
        ViewController *vc = [ViewController new];
        vc.model = model;
        
        [self.navigationController pushViewController:vc animated:YES];
    };
  
}



@end
